package com.qeng.aiwanba.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import java.util.Map;

/**
 * Created by lixy on 2017/9/22.
 */

public class SharedPreferencesUtil {
    private static final String JS_SP_NAME = "jsValueSp";
    private static final String APP_SP_NAME = "appValueSp";

    public static void setJsValue(Context context,String key,Object value){
        Log.e("tag","setJsValue:"+key+" "+value);
        setValue(context,JS_SP_NAME,key,value);
    }

    public static Object getJsValue(Context context,String key,Object defaultValue){
        return getValue(context,JS_SP_NAME,key,defaultValue);
    }

    public static Map<String,?> getAllJsValue(Context context){
        SharedPreferences sp = context.getSharedPreferences(JS_SP_NAME,
                Context.MODE_PRIVATE);
        return sp.getAll();
    }

    public static void removeJsValue(Context context,String key){
        removeVaule(context,JS_SP_NAME,key);
    }

    public static void removeJsAllValue(Context context){
        removeAllVaule(context,JS_SP_NAME);
    }

    public static void setAppValue(Context context,String key,Object value){
        setValue(context,APP_SP_NAME,key,value);
    }

    public static Object getAppValue(Context context,String key,Object defaultValue){
        return getValue(context,APP_SP_NAME,key,defaultValue);
    }

    public static void removeAppValue(Context context,String key){
        removeVaule(context,APP_SP_NAME,key);
    }

    public static void removeAppAllValue(Context context){
        removeAllVaule(context,APP_SP_NAME);
    }

    private static void setValue(Context context,String spName,String key,Object value){
        SharedPreferences sharedPreferences = context.getSharedPreferences(spName,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        if (value == null || key == null) {
            return;
        }
        String type = value.getClass().getSimpleName();

        if (String.class.getSimpleName().equals(type)) {
            editor.putString(key, (String) value);
        } else if (Integer.class.getSimpleName().equals(type)) {
            editor.putInt(key, (Integer) value);
        } else if (Boolean.class.getSimpleName().equals(type)) {
            editor.putBoolean(key, (Boolean) value);
        } else if (Float.class.getSimpleName().equals(type)) {
            editor.putFloat(key, (Float) value);
        } else if (Double.class.getSimpleName().equals(type)) {
            editor.putFloat(key, Float.parseFloat(String.valueOf(value)));
        } else if (Long.class.getSimpleName().equals(type)) {
            editor.putLong(key, (Long) value);
        }
        editor.commit();
    }

    private static Object getValue(Context context,String spName, String key,
                                  Object defaultObject) {
        String type = defaultObject.getClass().getSimpleName();
        SharedPreferences sp = context.getSharedPreferences(spName,
                Context.MODE_PRIVATE);

        if (String.class.getSimpleName().equals(type)) {
            return sp.getString(key, (String) defaultObject);
        } else if (Integer.class.getSimpleName().equals(type)) {
            return sp.getInt(key, (Integer) defaultObject);
        } else if (Boolean.class.getSimpleName().equals(type)) {
            return sp.getBoolean(key, (Boolean) defaultObject);
        } else if (Float.class.getSimpleName().equals(type)) {
            return sp.getFloat(key, (Float) defaultObject);
        } else if (Double.class.getSimpleName().equals(type)) {
            float result = sp.getFloat(key,
                    Float.parseFloat(String.valueOf(defaultObject)));
            return Double.parseDouble(String.valueOf(result));
        } else if (Long.class.getSimpleName().equals(type)) {
            return sp.getLong(key, (Long) defaultObject);
        }
        return null;
    }

    private static void removeVaule(Context context,String spName,String key){
        SharedPreferences sp = context.getSharedPreferences(spName,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.commit();
    }

    private static void removeAllVaule(Context context,String spName){
        SharedPreferences sp = context.getSharedPreferences(spName,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
    }
}
